﻿<%@ Page Language="C#" MasterPageFile="/_layouts/NintexWorkflow/ActivityUIMasterPage.master" AutoEventWireup="true" CodeBehind="RunReportNWActionDialog.aspx.cs" Inherits="net.Windward.NWAdapters.RunReportNWActionDialog, Windward.RunReportNWAction2007, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1335a5e5c2db2403" EnableEventValidation="false"%>

<%@ Import Namespace="System.Data" %>

<asp:Content ID="ContentHeader" ContentPlaceHolderID="AdditionalPageHeader" runat="server">
    <script type="text/javascript" language="javascript">

    var reportDefinitionCtrl;
    var reportDefinitionXPath = "/NWActionConfig/Parameters/Parameter[@Name='ReportDefinition']/PrimitiveValue/@Value";
    var lblRptDef;
    var saveInSiteCtrl;
    var saveInSiteXPath = "/NWActionConfig/Parameters/Parameter[@Name='SaveInSiteUrl']/PrimitiveValue/@Value";
    var saveInDocLibCtrl;
    var saveInDocLibXPath = "/NWActionConfig/Parameters/Parameter[@Name='SaveInDocLibUrl']/PrimitiveValue/@Value";
    var lblSaveInDocLib;
    var runForQueryCtrl;
    var runForQueryXPath = "/NWActionConfig/Parameters/Parameter[@Name='RunForQuery']/PrimitiveValue/@Value";
    var parameterXmlXPath = "/NWActionConfig/Parameters/Parameter[@Name='ParameterXml']/PrimitiveValue/@Value";
    var runForConnStrCtrl;
    var runForConnStrXPath = "/NWActionConfig/Parameters/Parameter[@Name='RunForConnStr']/PrimitiveValue/@Value";
    var runForAdapterCtrl;
    var runForAdapterXPath = "/NWActionConfig/Parameters/Parameter[@Name='RunForAdapter']/PrimitiveValue/@Value";
    var runForEmailCtrl;
    var runForEmailXPath = "/NWActionConfig/Parameters/Parameter[@Name='RunForEmail']/PrimitiveValue/@Value";
    var mapEmailColCtrl;
    var mapEmailColXPath = "/NWActionConfig/Parameters/Parameter[@Name='IsMapEmailCol']/PrimitiveValue/@Value";
    var outFormatCtrl;
    var outFormatXPath = "/NWActionConfig/Parameters/Parameter[@Name='OutputFormat']/PrimitiveValue/@Value";
    var OutFormats = {DOCX:0, SML:1, PDF:2, PPTX:3, RTF:4, WML:5, XLS:6, XLSX:7};
    var mailServerCtrl;
    var mailServerXPath = "/NWActionConfig/Parameters/Parameter[@Name='MailServer']/PrimitiveValue/@Value";
    var mailFromCtrl;
    var mailFromXPath = "/NWActionConfig/Parameters/Parameter[@Name='MailFrom']/PrimitiveValue/@Value";
    var mailSubjectCtrl;
    var mailSubjectXPath = "/NWActionConfig/Parameters/Parameter[@Name='MailSubject']/PrimitiveValue/@Value";
    var mailBodyCtrl;
    var mailBodyXPath = "/NWActionConfig/Parameters/Parameter[@Name='MailBody']/PrimitiveValue/@Value";
    var mapNames = [];
    var mapTypes = [];
    var values = [];
    var mapCols = [];

    function TPARetrieveConfig()
    {
        //debugger;
        getControls();
        
        // do not overwrite with old config values on post back
        if(!isPostBack)
        {
            //debugger;
            
            // read the values from the config xml
            //debugger;
            reportDefinitionCtrl.value = configXml.selectSingleNode(reportDefinitionXPath).text;
            saveInSiteCtrl.value = configXml.selectSingleNode(saveInSiteXPath).text;
            saveInDocLibCtrl.value = configXml.selectSingleNode(saveInDocLibXPath).text;
            runForQueryCtrl.innerText = configXml.selectSingleNode(runForQueryXPath).text;
            runForConnStrCtrl.value = configXml.selectSingleNode(runForConnStrXPath).text;
            runForEmailCtrl.value = configXml.selectSingleNode(runForEmailXPath).text;
            mapEmailColCtrl.checked = (configXml.selectSingleNode(mapEmailColXPath).text == "true");
            mailServerCtrl.value = configXml.selectSingleNode(mailServerXPath).text;
            mailFromCtrl.value = configXml.selectSingleNode(mailFromXPath).text;
            mailSubjectCtrl.value = configXml.selectSingleNode(mailSubjectXPath).text;
            mailBodyCtrl.value = configXml.selectSingleNode(mailBodyXPath).text;
            
            // determine which adapter was selected
            //debugger;
            var adapterText = configXml.selectSingleNode(runForAdapterXPath).text;
            if (adapterText == "ODBC")
            {
                var rblMssql = document.getElementById("<%= rblRunForAdapter.ClientID %>_0");
                rblMssql.checked = false;
                var rblOdbc = document.getElementById("<%= rblRunForAdapter.ClientID %>_1");
                rblOdbc.checked = true;
            }
            else if (adapterText == "OLEDB")
            {
                var rblMssql = document.getElementById("<%= rblRunForAdapter.ClientID %>_0");
                rblMssql.checked = false;
                var rblOdbc = document.getElementById("<%= rblRunForAdapter.ClientID %>_2");
                rblOdbc.checked = true;
            }
            
            // determine which output format was selected
            var selectedValue = configXml.selectSingleNode(outFormatXPath).text;
            if (selectedValue != null && selectedValue != "")
                outFormatCtrl.selectedIndex = OutFormats[selectedValue];
            
            // load parms if we already have the report definition
            if (reportDefinitionCtrl.value != null && reportDefinitionCtrl.value.length > 0)
                updateRptrParms();
        }
        lblRptDef.innerHTML = reportDefinitionCtrl.value;
        lblSaveInDocLib.innerHTML = saveInDocLibCtrl.value;
    }

    function TPAWriteConfig()
    {
        //debugger;
        getControls();
        
        // write the values to the configXml
        configXml.selectSingleNode(reportDefinitionXPath).text = reportDefinitionCtrl.value;
        configXml.selectSingleNode(saveInSiteXPath).text = saveInSiteCtrl.value;
        configXml.selectSingleNode(saveInDocLibXPath).text = saveInDocLibCtrl.value;
        configXml.selectSingleNode(runForQueryXPath).text = runForQueryCtrl.innerText;
        configXml.selectSingleNode(runForConnStrXPath).text = runForConnStrCtrl.value;
        configXml.selectSingleNode(runForEmailXPath).text = runForEmailCtrl.value;
        configXml.selectSingleNode(mapEmailColXPath).text = mapEmailColCtrl.checked ? "true" : "false";
        configXml.selectSingleNode(mailServerXPath).text = mailServerCtrl.value;
        configXml.selectSingleNode(mailFromXPath).text = mailFromCtrl.value;
        configXml.selectSingleNode(mailSubjectXPath).text = mailSubjectCtrl.value;
        configXml.selectSingleNode(mailBodyXPath).text = mailBodyCtrl.value;
        
        // determine which adapter is selected
        if (document.getElementById("<%= rblRunForAdapter.ClientID %>_0").checked)
        {
            configXml.selectSingleNode(runForAdapterXPath).text = "MS SQL";
        }
        else if (document.getElementById("<%= rblRunForAdapter.ClientID %>_1").checked)
        {
            configXml.selectSingleNode(runForAdapterXPath).text = "ODBC";
        }
        else if (document.getElementById("<%= rblRunForAdapter.ClientID %>_2").checked)
        {
            configXml.selectSingleNode(runForAdapterXPath).text = "OLEDB";
        }
        
        // write the output format
        configXml.selectSingleNode(outFormatXPath).text = outFormatCtrl.options[outFormatCtrl.selectedIndex].value;
        
        //debugger;
        // write the parms
        configXml.selectSingleNode(parameterXmlXPath).text = "";
        for (var i=0; i < mapNames.length; i++)
        {
            // get the ith var and write it to configXml
            var mapName = document.getElementById(mapNames[i]).innerText;
            var mapType = document.getElementById(mapTypes[i]).value;
            var mapValue = document.getElementById(values[i]).value;
            var mapCol = document.getElementById(mapCols[i]).checked;
            configXml.selectSingleNode(parameterXmlXPath).text += mapType + ":" + mapName + "=" + mapValue + ":" + mapCol + ";";
            //debugger;
        }
        
        
        return true;
    }
    
    function getControls()
    {
        //debugger;
        
        // get the controls
        reportDefinitionCtrl= document.getElementById("<%= txtRptDef.ClientID %>");
        lblRptDef = document.getElementById("<%= lblRptDef.ClientID %>");
        saveInSiteCtrl= document.getElementById("<%= txtSaveInSite.ClientID %>");
        saveInDocLibCtrl= document.getElementById("<%= txtSaveInDocLib.ClientID %>");
        lblSaveInDocLib = document.getElementById("<%= lblSaveInDocLib.ClientID %>");
        runForQueryCtrl = document.getElementById("<%= txtRunForQuery.ClientID %>");
        runForConnStrCtrl = document.getElementById("<%= txtRunForConnStr.ClientID %>");
        runForAdapterCtrl = document.getElementById("<%= rblRunForAdapter.ClientID %>");
        runForEmailCtrl = document.getElementById("<%= txtRunForEmail.ClientID %>");
        mapEmailColCtrl = document.getElementById("<%= chkMapEmailCol.ClientID %>");
        outFormatCtrl = document.getElementById("<%= ddlOutFormat.ClientID %>");
        mailServerCtrl = document.getElementById("<%= txtMailServer.ClientID %>");
        mailFromCtrl = document.getElementById("<%= txtMailFrom.ClientID %>");
        mailSubjectCtrl = document.getElementById("<%= txtMailSubject.ClientID %>");
        mailBodyCtrl = document.getElementById("<%= txtMailBody.ClientID %>");
    }
    
    function updateRptrParms() 
    {
        //debugger;
        mapNames = [];
        mapTypes = [];
        values = [];
        mapCols = [];
    
        // force a postback with __EVENTARGUMENT="UPDATE_PARMS"
        <%= UpdateRptrParms %>
    }
    </script>
    
    <base target="_self" />
</asp:Content>

<asp:Content ID="ContentBody" ContentPlaceHolderID="NintexPropertyPlaceHolder" runat="Server">

    <div class="RowBlue">
        <asp:Label ID="lblTopMessage" runat="server" />
    </div>

    <div class="RowBlue">
        <span class="Label req">Select a report definition</span>
        <asp:Menu ID="mnuReportDefinition" runat="server" OnMenuItemClick="mnuReportDefinition_Click" />
        <span>You selected:&nbsp;</span><asp:Label ID ="lblRptDef" runat="server" />
        <input type="hidden" class="InputWidth" id="txtRptDef" runat="server" />
    </div>
    
    <div class="RowBlue">
        <span class="Label req">Select a document library to save reports in</span>
        <asp:Menu ID="mnuSaveInDocLib" runat="server" OnMenuItemClick="mnuSaveInDocLib_Click" />
        <span>You selected:&nbsp;</span><asp:Label ID="lblSaveInDocLib" runat="server" />
        <input type="hidden" class="InputWidth" id="txtSaveInSite" runat="server" />
        <input type="hidden" class="InputWidth" id="txtSaveInDocLib" runat="server" />
    </div>
    
    <div class="RowBlue">
        <span class="Label req">Output Format</span>
        <select id="ddlOutFormat" runat="server">
            <option value="DOCX">DOCX</option>
            <option value="SML">Excel 2003 XML</option>
            <option value="PDF">PDF</option>
            <option value="PPTX">PPTX</option>
            <option value="RTF">RTF</option>
            <option value="WML">Word 2003 XML</option>
            <option value="XLS">XLS</option>
            <option value="XLSX">XLSX</option>
        </select>
    </div>
    
    <div class="RowBlue">
        <table>
            <tr>
                <td><span class="Label">Connection String</span></td>
                <td><input type="text" class="InputWidth" id="txtRunForConnStr" runat="server" /></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <asp:RadioButtonList ID="rblRunForAdapter" runat="server" RepeatDirection="Horizontal" >
                        <asp:ListItem Selected="True">MS SQL</asp:ListItem>
                        <asp:ListItem>ODBC</asp:ListItem>
                        <asp:ListItem>OLEDB</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
            </tr>
        </table>
    </div>
    <div class="RowBlue">
        <span class="Label">Run-For Query</span>
        <textarea class="InputWidth" rows="4" id="txtRunForQuery" runat="server" />
    </div>
    <div class="RowBlue">
        <span class="Label">Mail Server</span>
        <input class="InputWidth" id="txtMailServer" runat="server" />
    </div>
    <div class="RowBlue">
        <span class="Label">From:</span>
        <input class="InputWidth" id="txtMailFrom" runat="server" />
    </div>
    <div class="RowBlue">
        <table>
            <tr>
                <td><span class="Label" style="text-align:left">Email To:</span></td>
                <td><span class="Label">Map To Column?</span></td>
            </tr>
            <tr>
                <td><input type="text" class="InputWidth" id="txtRunForEmail" runat="server" /></td>
                <td style="text-align:center"><input type="checkbox" id="chkMapEmailCol" runat="server" /></td>
            </tr>
        </table>
    </div>
    <div class="RowBlue">
        <span class="Label">Subject:</span>
        <input class="InputWidth" id="txtMailSubject" runat="server" />
    </div>
    <div class="RowBlue">
        <span class="Label">Body:</span>
        <input class="InputWidth" id="txtMailBody" runat="server" />
    </div>
    
    <div class="RowBlue">
        <asp:Repeater ID="rptrParms" runat="server" OnItemDataBound="rptrParms_ItemDataBound" >
            <HeaderTemplate>
                <table style="width:100%;text-align:left;">
                    <tr>
                        <th>Variable Name</th>
                        <th colspan="2">Input or Select Variable Data</th>
                        <th>Map To Column?</th>
                    </tr>
            </HeaderTemplate>
            
            <ItemTemplate>
                <tr>
                    <td><asp:Label ID="mapName" runat="server" OnDataBinding="TemplateControl_DataBinding" /><asp:HiddenField ID="mapType" runat="server" OnDataBinding="TemplateControl_DataBinding" /></td>
                    <td>
                        <asp:MultiView ID="multiView" runat="server" ActiveViewIndex="0" OnDataBinding="TemplateControl_DataBinding">
                            <asp:View ID="viewTextBox" runat="server">
                                <asp:TextBox ID="varText" runat="server" />
                            </asp:View>
                            <asp:View ID="viewCalendar" runat="server">
                                <asp:Calendar ID="varCal" runat="server" />
                            </asp:View>
                        </asp:MultiView>
                    </td>
                    <td><asp:Label ID="mapDesc" runat="server" OnDataBinding="TemplateControl_DataBinding" /></td>
                    <td style="text-align:center"><asp:CheckBox ID="mapCol" runat="server" /></td>
                </tr>
            </ItemTemplate>
            
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </div>
    
</asp:Content>
